package com.kaili.basic.controlle;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseTransferAddDTO;
import com.kaili.basic.pojo.dto.BaseTransferLikeDTO;
import com.kaili.basic.pojo.dto.BaseTransferQueryDTO;
import com.kaili.basic.pojo.entity.BaseTransferManage;
import com.kaili.basic.pojo.vo.BaseTransferVO;
import com.kaili.basic.service.BaseTransferManageService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Objects;


@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Validated
@Api(tags = "中转商管理", description = "中转商管理相关API")
public class BaseTransferManageController {

    private final BaseTransferManageService transferManageService;

    @Autowired
    public BaseTransferManageController(BaseTransferManageService transferManageService) {
        this.transferManageService = transferManageService;
    }

    @PostMapping("/transfer")
    @ApiOperation(value = "新增中转商")
    public RestStatus addTransferManage(@Validated @RequestBody BaseTransferAddDTO baseTransferAddDTO) {
        BaseTransferManage transferManage = new BaseTransferManage();
        BeanUtils.copyProperties(baseTransferAddDTO, transferManage);
        List<BaseTransferManage> list = transferManageService.findTransByTransferNameAndContacts(transferManage);
        if (list.isEmpty()) {
            transferManageService.addTrans(transferManage);
            return CommonStatus.ADD_OK;
        }
        return CommonStatus.ADD_DEFEAT;
    }

    @DeleteMapping("/transfer/{transferId}")
    @ApiOperation(value = "删除中转商")
    public RestStatus deleteTransferManage(@NotNull(message = "中转商ID不能为空") @PathVariable("transferId") Long transferId) {
        BaseTransferManage tm = transferManageService.getTransByTid(transferId);
        if (tm != null) {
            transferManageService.deleteTrans(tm.getTransferId());
            return CommonStatus.DELETE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @PutMapping("/transfer")
    @ApiOperation(value = "更新中转商")
    public RestStatus updateTransferManage(@Validated @RequestBody BaseTransferAddDTO baseTransferAddDTO) {
        if (baseTransferAddDTO == null) {
            return CommonStatus.UPDATE_FAIL;
        }
        BaseTransferManage transferManage = new BaseTransferManage();
        BeanUtils.copyProperties(baseTransferAddDTO, transferManage);
        List<BaseTransferManage> list = transferManageService.findTransByTransferNameAndContacts(transferManage);
        for (BaseTransferManage t : list){
            if (!Objects.equals(t.getTransferId(), transferManage.getTransferId())) {
                return CommonStatus.ADD_DEFEAT_TRANS;
            }
        }
        transferManageService.updateTrans(transferManage);
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping("/transfer")
    @ApiOperation(value = "分页查询中转商")
    public Page<BaseTransferManage> findTransBy(@ModelAttribute  BaseTransferQueryDTO transferQueryDTO) {
        BaseTransferManage transferManage = new BaseTransferManage();
        BeanUtils.copyProperties(transferQueryDTO, transferManage);
        if(LoginDetailUtils.getSiteId() != null && LoginDetailUtils.getSiteId()==1){
            transferManage.setSiteId(null);
        }
        Page<BaseTransferManage> page = transferManageService.findTransBy(new Page<BaseTransferManage>(transferManage.getPage(), transferManage.getPageSize()), transferManage);
        for (BaseTransferManage btm : page.getRecords()) {
            String result = btm.getTransferId().toString();
            switch (result.length()) {
                case 1 :
                    result = "000" + btm.getTransferId().toString();
                    break;
                case 2 :
                    result = "00" + btm.getTransferId().toString();
                    break;
                case 3 :
                    result = "0" + btm.getTransferId().toString();
                    break;
                case 4 :
                    result = btm.getTransferId().toString();
                    break;
                default :
                    result = "-1";
                    break;
            }
            btm.setTransferCode(result);
        }
        return page;
    }

    @GetMapping("/transfer/{transferId}")
    @ApiOperation(value = "根据中转商编号查询中转商信息")
    public BaseTransferManage getTransByTid(@NotEmpty(message = "中转商ID不能为空") @PathVariable("transferId") Long transferId){
        return transferManageService.getTransByTid(transferId);
    }

    @GetMapping("/transferManage/{transferName}")
    @ApiOperation(value = "中转商名称模糊查询")
    public List<BaseTransferVO> likeTransferId(@NotEmpty(message = "中转商不能为空") @PathVariable("transferName") String transferName) {
        return transferManageService.getTransLikeTransferId(transferName);
    }

    @GetMapping("/transfer/id")
    @ApiOperation(value = "获取最新中转商id")
    public String getTransferIdForList() {
        List<Long> list = transferManageService.getTransferIdForList();
        Long id = 0L;
        if(list!=null && list.size()>0) {
            id = Collections.max(list) + 1;
        }else{
            id++;
        }

        String result;
        switch (id.toString().length()) {
            case 1 :
                result = "000" + id.toString();
                break;
            case 2 :
                result = "00" + id.toString();
                break;
            case 3 :
                result = "0" + id.toString();
                break;
            case 4 :
                result = id.toString();
                break;
            default :
                result = "-1";
                break;
        }
        return result;
    }
}
